package com.blockmeta.bbs.overallserviceapplication.search.fragment;

import android.os.Bundle;

import com.blockmeta.bbs.baselibrary.net.callback.Callback;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.businesslibrary.base.fragment.BaseLazyBusinessListFragment;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.net.okhttp.AdapterLoadDataBusiness;
import com.blockmeta.bbs.businesslibrary.net.okhttp.OKHttpGetReq;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.overallserviceapplication.pojo.WeiBoPOJO;
import com.blockmeta.bbs.overallserviceapplication.search.adapter.WeiBoListAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

/**
 * Created by author on 2018/5/10.
 */

public class WeiboListFragment extends BaseLazyBusinessListFragment<List<WeiBoPOJO>> {

    private int page = 1;

    public static WeiboListFragment newInstance(String key, String value) {
        WeiboListFragment fragment = new WeiboListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.ZERO, key);
        bundle.putString(Constant.ONE, value);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        return new WeiBoListAdapter(R.layout.item_list_weibo, null, mContext);
    }

    @Override
    public String getUrl() {
        return NetConstant.NEWS_WEIBO + "/" + page;
    }

    @Override
    public HashMap<String, String> initHashMap() {
        HashMap<String, String> map = new HashMap<>();
        if (getArguments()!=null){
            map.put(getArguments().getString(Constant.ZERO), getArguments().getString(Constant.ONE));
        }
        return map;
    }

    @Override
    public List<WeiBoPOJO> parseData(String resp) {
        List<WeiBoPOJO> listWeibo;
        Type collectionType = new TypeToken<List<WeiBoPOJO>>() {
        }.getType();
        try {
            listWeibo = GsonUtil.buildGson().fromJson(resp, collectionType);
        } catch (Exception e) {
            listWeibo = null;
        }
        return listWeibo;
    }

    @Override
    protected void adapterSetData() {
        mAdapter.setNewData(mPOJO);
    }

    @Override
    protected void adapterAddData() {
        if (mPOJO == null) {
            list_swipe.finishLoadmore();
            return;
        }
        mAdapter.addData(mPOJO);
    }


    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        String url = NetConstant.NEWS_WEIBO+"/"+page;
       initWeiboNet(url,true);
    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page=1;
        String url = NetConstant.NEWS_WEIBO+"/"+page;
        initWeiboNet(url,false);
    }

    private void initWeiboNet(String url, final boolean isLoadMore) {
        mGetReq = new OKHttpGetReq(mContext, url);

        mGetReq.getDataWithParam(new Callback() {
            @Override
            public void onError(String resp) {
                AdapterLoadDataBusiness.LoadErrorData(mContext, mAdapter, list_recy, new AdapterLoadDataBusiness.ReLoadNet() {
                    @Override
                    public void retryLoadNet() {
                        initNet(false);
                    }
                });
            }

            @Override
            public void onResponse(String resp, int id) {
                AdapterLoadDataBusiness.finishLoadAndRefresh(list_swipe);
                processData(resp, isLoadMore);
            }
        }, initHashMap());
    }

}
